﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Security;

namespace UsefulClasses
{
    public static class WebConnection
    {
        public static string GetResponce(string url)
        {
            string data = "";
            try
            {
                WebRequest request = WebRequest.Create(url);
                request.Proxy = null;
                request.Credentials = CredentialCache.DefaultCredentials;

                WebResponse response = request.GetResponse();
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);

                data = reader.ReadToEnd();

                reader.Close();
                response.Close();
            }
            catch
            { }

            return data;
        }

        public static string GetResponce(string url, string username, string password)
        {
            string data = "";
            try
            {
                WebRequest request = WebRequest.Create(url);
                request.Proxy = null;

                request.PreAuthenticate = true;
                request.Credentials = new NetworkCredential(username, password);

                WebResponse response = request.GetResponse();
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);

                data = reader.ReadToEnd();

                reader.Close();
                response.Close();
            }
            catch
            { }

            return data;
        }
    }
}
